package cn.ncupyh;

import java.sql.*;

public class JDBCPrestmt {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载并注册数据库驱动
        Class.forName("com.mysql.jdbc.Driver");
        String uri = "jdbc:mysql://127.0.0.1:3306/21soft?serverTimezone=GMT%2B8&userSSL=false";
        String root = "root";
        String password = "djx020601";
        //2.获执行者对象
        Connection conn = DriverManager.getConnection(uri,root,password);
        //3.书写SQL语句
        String sql = "select * from user where uname=?";
        //4.获取取数据库连接
//        Connection conn = getConnection(uri, root, password);
////        Statement pstmt = conn.createStatement();
        PreparedStatement pstmt = conn.prepareStatement(sql);
        //5.设置参数
        pstmt.setString(1,"zhangsan");
        //6.执行SQL语句
        ResultSet rs = pstmt.executeQuery();
        //7.处理结果集
        while(rs.next()){
            int uid = rs.getInt(1);
            String uname = rs.getString(2);
            String passwoed = rs.getString(3);
            System.out.println(uid+"\t"+uname+"\t"+password);
        }
        //8.关闭资源
        rs.close();
        pstmt.close();
        conn.close();
    }
}
